﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DWEIP.Models;
using DWEIP.Models.Sys;
using DWEIP.Models.tam;
using DataWin.Core.Model;
using DataWin.Core.Util;

namespace DWEIP.BLL.Sys
{
    public partial class SysUserBLL : BasicBLL
    {
        #region DAL
        public int DAL_GetUserCountByDepId(string depId)
        {
            //return Context.P_Sys_GetUserCountByDepId(depId).Cast<int>().First();
            return 0;
        }
        /*
        public IQueryable<SysRole> GetRefSysRole(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                return from m in Context.SysUser
                       from f in m.SysRole
                       where m.Id == id
                       select f;
            }
            return null;
        }

        public IQueryable<P_Sys_GetRoleByUserId_Result> GetRoleByUserId(string userId)
        {
            return Context.P_Sys_GetRoleByUserId(userId).AsQueryable();
        }
        public IQueryable<SysUser> GetListByPosId(string posId)
        {
            return Context.SysUser.Where(a => a.PosId == posId);
        }
        public IQueryable<P_Sys_GetUserByDepId_Result> GetUserByDepId(string depId)
        {
            return Context.P_Sys_GetUserByDepId(depId).AsQueryable();
        }

        public void UpdateSysRoleSysUser(string userId, string[] roleIds)
        {
            Context.P_Sys_DeleteSysRoleSysUserByUserId(userId);
            foreach (string roleid in roleIds)
            {
                if (!string.IsNullOrWhiteSpace(roleid))
                {
                    Context.P_Sys_UpdateSysRoleSysUser(roleid, userId);
                }
            }
            this.SaveChanges();
        }

        public IQueryable<P_Sys_GetAllUsers_Result> GetAllUsers()
        {
            return Context.P_Sys_GetAllUsers().AsQueryable();
        }
        */

        /// <summary>
        /// 根据ID获取一个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DAL_GetNameById(string id)
        {
            Table _tam05 = new Table("tam05");
            _tam05.SQL.Value = @"
SELECT tam05.* FROM tam05 where ae01=@id";
            _tam05.Params.Clear();
            _tam05.Params.Add("id", new Data(id));
            BLLSession.Load(_tam05);

            if (_tam05.Count > 0)
            {
                return _tam05["ae02"].AsString;
            }
            return "";
        }

        #endregion DAL




        public List<permModel> GetPermission(string UserId, string controller)
        {
            if (BLLSession == null)
            {
                return null;
            }

            BLLSession.BeginTransaction();
            Table _tam06 = new Table("tam06");
            _tam06.SQL.Value = @"
SELECT tam06.* FROM tam06 where af01=@curruser AND af02=@currprog";
            _tam06.Params.Clear();
            _tam06.Params.Add("curruser", new Data(UserId));
            _tam06.Params.Add("currprog", new Data("A00034"));//先不和controller连接
            BLLSession.Load(_tam06);
            BLLSession.Commit();

            if (_tam06.Count>0)
            {
                List<permModel> permList = new List<permModel>();

                permModel perm = new permModel();
                perm.KeyCode = "Create";
                perm.IsValid = (_tam06["af030"].AsString == "Y");
                permList.Add(perm);

                perm = new permModel();
                perm.KeyCode = "Edit";
                perm.IsValid = (_tam06["af031"].AsString == "Y");
                permList.Add(perm);

                perm = new permModel();
                perm.KeyCode = "Save";
                perm.IsValid = (_tam06["af031"].AsString == "Y");
                permList.Add(perm);

                perm = new permModel();
                perm.KeyCode = "Delete";
                perm.IsValid = (_tam06["af032"].AsString == "Y");
                permList.Add(perm);

                perm = new permModel();
                perm.KeyCode = "Query";
                perm.IsValid = (_tam06["af033"].AsString == "Y");
                permList.Add(perm);

                return permList;
            }

            return null;
        }

        public List<SysUser> GetListByDepId(string id)
        {
            if (BLLSession == null)
            {
                return null;
            }

            BLLSession.BeginTransaction();
            Table _tam05 = new Table("tam05");
            _tam05.SQL.Value = @"
SELECT tam05.* FROM tam05 where ae05=@currdept";
            _tam05.Params.Clear();
            _tam05.Params.Add("currdept", new Data(id));
            BLLSession.Load(_tam05);
            BLLSession.Commit();

            List<SysUser> userList = new List<SysUser>();
            if (_tam05.Count > 0)
            {
                foreach (IRow Row in _tam05)
                {
                    SysUser sysUser = new SysUser();
                    sysUser.Id = Row["ae01"].AsString;
                    sysUser.UserName = Row["ae03"].AsString;
                    sysUser.Password = Row["ae04"].AsString;
                    sysUser.TrueName = Row["ae02"].AsString;
                    sysUser.Card = "";
                    sysUser.MobileNumber = Row["ae_13"].AsString;
                    sysUser.PhoneNumber = Row["ae_12"].AsString;
                    sysUser.QQ = "";
                    sysUser.EmailAddress = Row["ae_10"].AsString;
                    sysUser.OtherContact = "";
                    sysUser.Province = "";
                    sysUser.City = "";
                    sysUser.Village = "";
                    sysUser.Address = "";
                    sysUser.State = true;
                    sysUser.CreateTime = Row["create_date"].AsDateTime;
                    sysUser.CreatePerson = Row["create_user"].AsString;
                    sysUser.Sex = "";
                    sysUser.Birthday = (new Data("19800101")).AsDateTime;
                    sysUser.JoinDate = (new Data("20000101")).AsDateTime;
                    sysUser.Marital = "";
                    sysUser.Political = "";
                    sysUser.Nationality = "";
                    sysUser.Native = "";
                    sysUser.School = "";
                    sysUser.Professional = "";
                    sysUser.Degree = "";
                    sysUser.DepId = Row["ae05"].AsString;
                    sysUser.PosId = "";
                    sysUser.Expertise = "";
                    sysUser.JobState = true;
                    sysUser.Photo = "";
                    sysUser.Attach = "";
                    sysUser.Lead = "";
                    sysUser.LeadName = "";
                    sysUser.IsSelLead = true;
                    sysUser.IsReportCalendar = true;
                    sysUser.IsSecretary = false;

                    userList.Add(sysUser);
                }
            }
            return userList;
        }
    }
}
